#include <QMessageBox>
#include <QTextDocument>
#include <QItemSelectionModel>

#include "singledictdialog.h"
#include "ui_singledictdialog.h"
#include "singlenewdictelementdialog.h"
#include "singledictmodel.h"
#include "logic.h"


SingleDictDialog::SingleDictDialog(const QString &dictNameI,
                                   const QString &sqlTableI, QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::SingleDictDialog), _sqlTable(sqlTableI), dictName(dictNameI)
{
    m_ui->setupUi(this);
    this->setWindowTitle(tr("Справочник \"\%1\"").arg(dictName));
    connect(m_ui->tvElements, SIGNAL(doubleClicked(QModelIndex)),
            this, SLOT(on_pbEdit_clicked()) );

    model = new SingleDictModel(sqlTableI, this);
    m_ui->tvElements->setModel(model);
    model->setSelectionModel(m_ui->tvElements->selectionModel());
    m_ui->tvElements->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_ui->tvElements->setEditTriggers(QAbstractItemView::NoEditTriggers);
   // m_ui->tvElements->hideColumn(0);
    m_ui->tvElements->sortByColumn(1, Qt::AscendingOrder);

    connect(m_ui->tvElements->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
            this, SLOT(selectCurrentRow(QModelIndex,QModelIndex)) );

    connect(m_ui->leFilter, SIGNAL(textChanged(QString)),
            this, SLOT(filtrate()) );

    int permissions = logic::permissions();
    if (permissions < 77) {
        m_ui->pbEdit->setDisabled(true);
        m_ui->pbDelete->setDisabled(true);
    }
    model->refresh();

}

SingleDictDialog::~SingleDictDialog()
{
    delete m_ui;
}

void SingleDictDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}


void SingleDictDialog::on_leFilter_textChanged(QString filter)
{
    model->filtrate(filter);
    m_ui->tvElements->selectRow(0);
}

void SingleDictDialog::on_pbAdd_clicked()
{
    SingleNewDictElementDialog newDialog(dictName, this);
    newDialog.setTableName(_sqlTable);
    newDialog.exec();
    model->refresh();
}


void SingleDictDialog::on_pbDelete_clicked()
{
    if (m_ui->tvElements->selectionModel()->selection().count() == 0)
        return;
    int row = m_ui->tvElements->selectionModel()->selectedIndexes().at(0).row();

    if( QMessageBox::question(this, tr("удаление записи"),
                              tr("Вы действительно хотите удалить запись <b>\"%1\"<\b>?").arg(
                                      Qt::escape(model->record(row).value("name").toString())),
                              QMessageBox::Yes | QMessageBox::No, QMessageBox::No)  == QMessageBox::Yes)
    {
        logic::deleteRowSingleDict(_sqlTable, model->record(row).value("id").toInt());
        model->refresh();
    }


}


void SingleDictDialog::on_pbEdit_clicked()
{
    if (m_ui->tvElements->selectionModel()->selection().count() == 0) return;
    int row = m_ui->tvElements->selectionModel()->selectedIndexes().at(0).row();
    int id = model->record(row).value("id").toInt();

    SingleNewDictElementDialog newDialog(dictName, this, id,
                                         model->record(row).value("name").toString() );
    newDialog.setTableName(_sqlTable);
    newDialog.exec();
    model->refresh();
}


void SingleDictDialog::filtrate()
{
    model->filtrate(m_ui->leFilter->text());
    m_ui->tvElements->selectRow(0);
}

void SingleDictDialog::selectCurrentRow( const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
   m_ui->tvElements->selectionModel()->select(QItemSelection(topLeft, bottomRight), QItemSelectionModel::Current);

}





























